package com.shunli.cterminal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.examples.core.mybatis.mapper.KyLambdaQueryWrapper;
import com.examples.core.mybatis.service.AbstractService;
import com.shunli.cterminal.entity.SystemResultRedEnvelope;
import com.shunli.cterminal.mapper.SystemResultRedEnvelopeMapper;
import com.shunli.cterminal.model.res.ResultRedEnvelopeVO;
import com.shunli.cterminal.service.ResultRedEnvelopeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: ResultRedEnvelopeServiceImpl
 * @Description: TODO
 * @Author: DAIYALU
 * @Date: 2025/6/5
 * @Version: 1.0
 */
@Slf4j
@Service
public class ResultRedEnvelopeServiceImpl extends AbstractService<SystemResultRedEnvelopeMapper, SystemResultRedEnvelope> implements ResultRedEnvelopeService {

    @Override
    public BigDecimal getRedEnvelopeAmount(BigDecimal orderAmount) {
        log.info("getRedEnvelopeAmount orderAmount:{}", orderAmount);
        if (orderAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return BigDecimal.ZERO;
        }
        for (ResultRedEnvelopeVO systemResultRedEnvelope : getList()) {
            if (orderAmount.compareTo(systemResultRedEnvelope.getMinAmount()) >= 0 && orderAmount.compareTo(systemResultRedEnvelope.getMaxAmount()) < 0) {
                log.info("getRedEnvelopeAmount systemResultRedEnvelope:{}", JSONUtil.toJsonStr(systemResultRedEnvelope));
                return systemResultRedEnvelope.getRedEnvelopeAmount();
            }
        }
        return BigDecimal.ZERO;
    }

    @Override
    public List<ResultRedEnvelopeVO> getList() {
        return BeanUtil.copyToList(list(new KyLambdaQueryWrapper<>()), ResultRedEnvelopeVO.class);
    }
}
